package com.luoqiu.yygh.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.luoqiu.yygh.common.result.Result;
import com.luoqiu.yygh.common.utils.MD5;
import com.luoqiu.yygh.model.hosp.HospitalSet;
import com.luoqiu.yygh.service.HospitalSetService;
import com.luoqiu.yygh.vo.hosp.HospitalSetQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.Random;


@Api(tags = "医院设置管理")
@RestController
@RequestMapping(value = "/admin/hosp/hospSet")
public class HospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    @ApiOperation(value = "获取所有的医院")
    @GetMapping(value = "/all")
    public Result<List<HospitalSet>> all() {
        return Result.ok(hospitalSetService.list());
    }

    @DeleteMapping(value = "{id}")
    public Result<Boolean> delete(@PathVariable Long id) {
        return Result.ok(hospitalSetService.removeById(id));
    }

    @PostMapping(value = "/page/{current}/{limit}")
    public Result<Page<HospitalSet>> page(@PathVariable("current") Long current, @PathVariable("limit") Long limit,
                                          @RequestBody(required = false) HospitalSetQueryVo queryVo) {
        Page<HospitalSet> p = new Page<>(current, limit);
        QueryWrapper<HospitalSet> queryWrapper = new QueryWrapper<>();
        Optional.ofNullable(queryVo.getHoscode()).ifPresent(e -> queryWrapper.eq("hoscode", e));
        Optional.ofNullable(queryVo.getHosname()).ifPresent(e -> queryWrapper.like("hosname", e));
        Page<HospitalSet> setPage = hospitalSetService.page(p, queryWrapper);
        return Result.ok(setPage);
    }

    @PostMapping(value = "/save")
    public Result<Object> save(@RequestBody HospitalSet hospitalSet) {
        hospitalSet.setStatus(1);
        Random r = new Random();
        String key = MD5.encrypt(System.currentTimeMillis() + "" + r.nextInt(1000));
        hospitalSet.setSignKey(key);
        hospitalSet.setCreateTime(new Date());
        hospitalSet.setUpdateTime(new Date());
        hospitalSet.setIsDeleted(0);
        boolean save = hospitalSetService.save(hospitalSet);
        return Result.ok(save);
    }

    @PostMapping(value = "/update")
    public Result<Object> update(@RequestBody HospitalSet hospitalSet) {
        hospitalSet.setUpdateTime(new Date());
        boolean b = hospitalSetService.updateById(hospitalSet);
        return Result.ok(b);
    }

    @PostMapping(value = "/batch/delete")
    public Result<Object> del(@RequestBody List<Long> ids) {
        boolean b = hospitalSetService.removeByIds(ids);
        return Result.ok(b);
    }

    @GetMapping(value = "/lock")
    public Result<Object> lock(Long id, Integer status) {
        HospitalSet hospitalSet = new HospitalSet();
        hospitalSet.setId(id);
        hospitalSet.setStatus(status);
        hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }

    @GetMapping(value = "/sendKey/{id}")
    public Result<Object> sendKey(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        // todo 发送短信
        return Result.ok();
    }
}
